Rsync is a fast and versatile command line utility that synchronizes files and folders between two locations over a remote shell.
With Rsync you can mirror data, create incremental backups and copy files between systems. When copying data, you may want to exclude one or more files or directories based on their name or location.
In this tutorial, we will show you how to exclude files and directories with rsync.
You should have a basic knowledge of how rsync works .
In the examples below, we will use rsync with the -a, option. This tells rsync to syncs directories recursively, transfer special and block devices and preserve symbolic links, modification times, group, ownership, and permissions.
When excluding files or directories you need to use their relative paths to the source directory.
There are two options to specify the files and directories you want to exclude:
.From a command line, using the --exclude option.
.From a file, using the --exclude-from option.
To exclude a specific file, pass the relative path to the file to the --exclude option.
In the following example the file src_directory/file.txt will not be transferred:
rsync -a --exclude 'file.txt' src_directory/ dst_directory/
Excluding a specific directory is same as excluding a file, just pass the relative path to the directory to the --exclude option as shown below:
rsync -a --exclude 'dir1' src_directory/ dst_directory/
If you want to exclude the directory content but not the directory itself use dir1/* instead of dir1:
rsync -a --exclude 'dir1/*' src_directory/ dst_directory/
To exclude multiple files or directories simply specify multiple --exclude options:
rsync -a --exclude 'file1.txt' --exclude 'dir1/*' --exclude 'dir2' src_directory/ dst_directory/
If you prefer to use a single --exclude option you can list the files and directories you want to exclude in curly braces {} separated by a comma as shown below:
rsync -a --exclude={'file1.txt','dir1/*','dir2'} src_directory/ dst_directory/
If the number of the files and/or directories you want to exclude is large, instead of using multiple --exclude options you can specify the files and directories you want to exclude in a file and pass the file to the --exclude-from option.
The command below does exactly the same as the one above:
rsync -a --exclude-from='exclude-file.txt' src_directory/ dst_directory/
exclude-file.txt
file1.txt
dir1/*
dir2
With rsync you can also exclude files and directories based on a pattern that matches the file or directory name.
For example, to exclude all .jpg files you would run:
rsync -a --exclude '*.jpg*' src_directory/ dst_directory/
It is little trickier to exclude all other files and directories except those that match a certain pattern. Let’s say you want to exclude all other files and directories except the files ending with .jpg.
One option is to use the following command:
rsync -a -m --include='*.jpg' --include='*/' --exclude='*' src_directory/ dst_directory/
When using multiple include/exclude option, the first matching rule applies.
.--include='*.jpg' - First we are including all .jpg files.
.--include='*/' - Then we are including all directories inside the in src_directory directory. Without this rsync will only copy *.jpg files in the top level directory.
.-m - Removes the empty directories.
Another option would be to pipe the output of the find command to rsync:
find src_directory/ -name "*.jpg" -printf %P\\0\\n | rsync -a --files-from=- src_directory/ dst_directory/
.-printf %P\\0\\n - will remove the src_directory/ from the file path.
.--files-from=- - means include only the files from the standard input (files passed from the find command).
In this tutorial, you learned how to exclude files and directories when transferring data with Rsync. There’s lots more to learn about Rsync at Rsync User’s Manual page.